import http from '../static/plugins/axios'
import vue from '../main'
import Vue from 'vue'

let interval = null

function stop() {
  if (interval == null) {
    return
  }
  clearInterval(interval)
  interval = null
}

function start() {
  stop()
  interval = setInterval(() => {
    http.get('um/user/info', null, true, true).then(rsp => {
      if (!rsp) {
        stop()
        vue.$router.push('/login')
      }
    })
  }, 5000)
}

export default {
  updateState(state, param) {
    state[param.prop] = param.value;
  },

  initSizes(state) {
    const height = window.innerHeight;
    const width = window.innerWidth;
    const loginHeight = 350;
    const loginWidth = 450;
    const navHead = state.showHeader ? 150 : 0;
    const head = 60;
    const pd = 40;
    const page = 37;
    Vue.set(state.sizes, 'fullHeight', height + 'px');
    Vue.set(state.sizes, 'loginHeight', loginHeight + 'px');
    Vue.set(state.sizes, 'loginWidth', loginWidth + 'px');
    Vue.set(state.sizes, 'loginTop', (height - loginHeight) * 0.4 + 'px');
    Vue.set(state.sizes, 'loginLeft', (width - loginWidth) * 0.49 + 'px');
    Vue.set(state.sizes, 'headHeight', navHead + 'px');
    Vue.set(state.sizes, 'mainHeight', height - navHead + 'px');
    Vue.set(state.sizes, 'tableHei', height - navHead - head - pd);
    Vue.set(state.sizes, 'pTableHei', state.sizes.tableHei - page);
  },

  getUserInfo(state) {
    http.get('um/user/info').then(response => {
        if (response) {
          state.userInfo = response
          start()
        } else {
          vue.$router.push('/login')
        }
      })
      .catch(() => {
        vue.$router.push('/login')
      })
  },

  queryMenus(state) {
    const tmp = [{
      title: '主页',
      id: 'home',
      href: 'home',
      children: []
    }];
    http.get('um/menu/tree').then(response => {
      if (response) {
        state.menuGroups = tmp.concat(response);
      } else {
        state.menuGroups = tmp;
      }
    }).catch(() => {
      state.menuGroups = tmp;
    });
  },

  queryRoleMap(state, refresh) {
    if (!refresh && state.roles != null) {
      return
    }
    http.get('public/role/map').then(function(response) {
      response = response == null ? [] : response;
      Vue.set(state, 'roles', response)
    })
  },

  getDictMap(state, codes, callBack) {
    if (codes == null) {
      return
    }
    const arr = []
    for (let i = 0; i < codes.length; i++) {
      if (Object.keys(state.dict).indexOf(codes[i]) == -1) {
        arr.push(codes[i])
      }
    }
    if (arr.length == 0) {
      return
    }
    http.post('public/get/dict', arr).then(function(response) {
      if (response) {
        for (let i = 0; i < arr.length; i++) {
          const key = arr[i]
          state.dict[key] = {}
          const dicts = response[key]
          if (dicts == null) {
            continue
          }
          for (let j = 0; j < dicts.length; j++) {
            const tmp = dicts[j]
            const num = parseInt(tmp.code)
            if (!isNaN(num)) {
              state.dict[key][num] = tmp.name
            } else {
              state.dict[key][tmp.code] = tmp.name
            }
          }
        }
        if (callBack != null) {
          callBack()
        }
      }
    })
  },

  logout(state) {
    http.get('um/login/out').then(response => {
      if (response) {
        vue.$router.push('/login')
        state.userInfo = {}
      }
    })
  },

  getActionPers(state, content) {
    http.get('public/my/action/per', {
      content: content
    }).then(function(response) {
      if (response) {
        state.buttons = {}
        for (let i = 0; i < response.length; i++) {
          state.buttons[response[i]] = true
        }
      }
    })
  }

}
